<?
    require_once("inc_security.php");
    //check quyền them sua xoa
    checkAddEdit("edit");
    ///echo '<pre>';print_r($_POST);echo '</pre>';
    $record_id 		= getValue("record_id", 'int', 'GET')?getValue("record_id", 'int', 'GET'):getValue("record_id", 'int', 'POST');
    
    $errorMsg = '';
    
    $action = getValue('action', 'str', 'POST');
    $after_save_data = getValue('after_save_data', 'str', 'POST', 'listing');
    
    $myform = new generate_form;
    $myform->addTable($fs_table);
    
    $arr_partners = array();
    $arr_experts = array();
    
	if($action == 'edit' && getValue('submit', 'str', 'POST', '') != ''){
        $int_name = getValue('int_name', 'str', 'POST', '');
        $int_summary = getValue('int_summary', 'str', 'POST', '');
        $int_body = getValue('int_body', 'str', 'POST', '');
        $partners = getValue('partners', 'arr', 'POST', array());
        $partners_id = getValue('partners_id', 'arr', 'POST', array());
        $partners_href = getValue('partners_href', 'arr', 'POST', array());
        $partners_logo = getValue('partners_logo', 'arr', 'POST', array());
        if($partners){
            foreach($partners as $key => $value){
                if($value != ''){
                    $arr_partners[] = array(
                        'pid' => $partners_id[$key],
                        'title' => $partners[$key],
                        'href' => $partners_href[$key],
                        'logo' => $partners_logo[$key],
                    );
                }
            }
        }
        $int_partners = serialize($arr_partners);
        
        $experts = getValue('experts', 'arr', 'POST', array());
        $experts_id = getValue('experts_id', 'arr', 'POST', array());
        $experts_href = getValue('experts_href', 'arr', 'POST', array());
        $experts_image = getValue('experts_image', 'arr', 'POST', array());
        $experts_role = getValue('experts_role', 'arr', 'POST', array());
        $experts_com_name = getValue('experts_com_name', 'arr', 'POST', array());
        $experts_com_href = getValue('experts_com_href', 'arr', 'POST', array());
        if($experts){
            foreach($experts as $key => $value){
                if($value != ''){
                    $arr_experts[] = array(
                        'eid' => $experts_id[$key],
                        'title' => $experts[$key],
                        'href' => $experts_href[$key],
                        'image' => $experts_image[$key],
                        'role' => $experts_role[$key],
                        'com_name' => $experts_com_name[$key],
                        'com_href' => $experts_com_href[$key],
                    );
                }
            }
        }
        $int_experts = serialize($arr_experts);
        
        $myform->removeHTML(0);
    	$myform->add('int_name', 'int_name', 0, 0, $int_name,1,translate_text('Chưa nhập tiêu đề'),0,'');
        $myform->add('int_summary', 'int_summary', 0, 0, $int_summary,1,translate_text('Chưa nhập giới thiệu tóm tắt'),0,'');
        $myform->add('int_body', 'int_body', 0, 0, $int_body,1,translate_text('Chưa nhập nội dung'),0,'');
        $myform->add('int_partners', 'int_partners', 0, 1, $int_partners);
        $myform->add('int_experts', 'int_experts', 0, 1, $int_experts);
		$errorMsg .= $myform->checkdata();
        
		if($errorMsg == ""){
            $upload		= new upload($fs_fieldupload, $fs_filepath, $fs_extension, $fs_filesize);
        	$filename	= $upload->file_name;
            if($filename){
                delete_file($fs_table, $id_field, $record_id, $fs_fieldupload, $fs_filepath);
                $myform->add('int_picture', 'filename', 0, 1, '', 1, translate_text('Chọn ảnh mô tả'), 0, '');
                foreach($arr_resize as $type => $arr){
                    resize_image($fs_filepath, $filename, $arr['width'], $arr['height'], $arr['quality'], $type);
                }
            }
            
            $db_ex = new db_execute($myform->generate_update_SQL($id_field, $record_id));
            unset($upload);
            
			// Redirect to add new
			switch($after_save_data){
                case 'edit':
                    //redirect('edit.php?record_id='.$record_id);
                    break;
                case 'listing':
                    redirect('listing.php');
                    break;
            }
			exit();
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="../../../css/jquery-ui-autocompelete.min.css" rel="stylesheet"/>
<?=$load_header?>
<script lang="javascript" src="../../../js/juimin/jquery.ui.core.min.js"></script>
<script lang="javascript" src="../../../js/juimin/jquery.ui.widget.min.js"></script>
<script lang="javascript" src="../../../js/juimin/jquery.ui.position.min.js"></script>
<script lang="javascript" src="../../../js/juimin/jquery.ui.menu.min.js"></script>
<script lang="javascript" src="../../../js/juimin/jquery.ui.autocomplete.min.js"></script>
<script lang="javascript" src="../../../js/custom.js"></script>
<? 
$myform->checkjavascript();
$errorMsg .= $myform->strErrorField;

//lay du lieu cua record can sua doi
$db_data 	= new db_query("SELECT * FROM " . $fs_table . " WHERE " . $id_field . " = " . $record_id);
$record = mysql_fetch_array($db_data->result);
if(!$arr_partners){
    $arr_partners = unserialize($record['int_partners']);
}
if(!$arr_experts){
    $arr_experts = unserialize($record['int_experts']);
}
?>
<style type="text/css">
<!--
.sitem{
    border-bottom: 1px solid #CCCCCC;
    border-left: 1px solid #CCCCCC;
    border-right: 1px solid #CCCCCC;
    cursor: pointer;
    margin-left: 5px;
    max-width: 250px;
    padding: 4px 10px;
}
.sitem:first-child{
    border-top: 1px solid #CCCCCC;
    margin-top: 5px;
}
-->
</style>
</head>
<body topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<? /*------------------------------------------------------------------------------------------------*/ ?>
<?=template_top(translate_text("Edit intro"))?>
<? /*------------------------------------------------------------------------------------------------*/ ?>
	<div style="margin: 0 auto; padding-left:30px;">
	<?
	$form = new form();
	$form->create_form("introform", $_SERVER["REQUEST_URI"], "post", "multipart/form-data",'onsubmit="validateForm(); return false;"');
	$form->create_table();
	?>
	<?=$form->text_note('Những ô dấu (<font class="form_asterisk">*</font>) là bắt buộc phải nhập.')?>
	<?=$form->errorMsg($errorMsg)?>	
    <?=$form->text('Tiêu đề', 'int_name', 'int_name', $record['int_name'], 'Tiêu đề', 1, 250, '', 255, '', '', '')?>
    <tr><td class="form_name">Ảnh cũ</td><td class="form_text"><? print '<img src="' . $fs_filepath . "medium_" . $record['int_picture'].' "border="0">'; ?></td></tr>
    <?=$form->getFile('Ảnh mô tả', 'int_picture', 'int_picture', 'Ảnh mô tả', 1, 30, '', '')?>
    
    <?php
    if(!empty($arr_partners)){
        foreach($arr_partners as $key => $value){
    ?>
        <tr class="partners-rows"><td class="form_name">Đối tác:</td><td>
        <input type="text" title="Đối tác" class="form_control search-partners" size="40" maxlength="255" style="width:250px;" placeholder="Tìm kiếm đối tác" name="partners[]" value="<?php print $value['title']; ?>"/>
        <input type="hidden" class="partner-id"  value="<?php print $value['pid']; ?>" name="partners_id[]"/>
        <input type="hidden" class="partner-href"  value="<?php print $value['href']; ?>" name="partners_href[]"/>
        <input type="hidden" class="partner-logo"  value="<?php print $value['logo']; ?>" name="partners_logo[]"/>
        </td></tr>
    <?php
        }
    }
    ?>
    <tr class="partners-rows"><td class="form_name">Đối tác:</td><td>
    <input type="text" title="Đối tác" class="form_control search-partners" size="40" maxlength="255" style="width:250px;" placeholder="Tìm kiếm đối tác" name="partners[]"/>
    <input type="hidden" class="partner-id" value="0" name="partners_id[]"/>
    <input type="hidden" class="partner-href" value="" name="partners_href[]"/>
    <input type="hidden" class="partner-logo" value="" name="partners_logo[]"/>
    </td></tr>
    <tr><td class="form_name"></td><td>
        <input type="button" value="Thêm đối tác" class="partners-add"/>
    </td></tr>
    <?php
    if(!empty($arr_experts)){
        foreach($arr_experts as $key => $value){
    ?>
        <tr class="expert-rows"><td class="form_name">Chuyên gia:</td><td>
        <input type="text" title="Chuyên gia" class="form_control search-expert" size="40" maxlength="255" style="width:250px;" placeholder="Tìm kiếm" name="experts[]" value="<?php print $value['title']; ?>"/>
        <input type="hidden" class="expert-id" value="<?php print $value['eid']; ?>" name="experts_id[]"/>
        <input type="hidden" class="expert-href" value="<?php print $value['href']; ?>" name="experts_href[]"/>
        <input type="hidden" class="expert-image" value="<?php print $value['image']; ?>" name="experts_image[]"/>
        <input type="hidden" class="expert-role" value="<?php print $value['role']; ?>" name="experts_role[]"/>
        <input type="hidden" class="expert-com-name" value="<?php print $value['com_name']; ?>" name="experts_com_name[]"/>
        <input type="hidden" class="expert-com-href" value="<?php print $value['com_href']; ?>" name="experts_com_href[]"/>
        </td></tr>
    <?php
        }
    }
    ?>
    <tr class="expert-rows"><td class="form_name">Chuyên gia:</td><td>
        <input type="text" title="Chuyên gia" class="form_control search-expert" size="40" maxlength="255" style="width:250px;" placeholder="Tìm kiếm" name="experts[]"/>
        <input type="hidden" class="expert-id" value="0" name="experts_id[]"/>
        <input type="hidden" class="expert-href" value="" name="experts_href[]"/>
        <input type="hidden" class="expert-image" value="" name="experts_image[]"/>
        <input type="hidden" class="expert-role" value="" name="experts_role[]"/>
        <input type="hidden" class="expert-com-name" value="" name="experts_com_name[]"/>
        <input type="hidden" class="expert-com-href" value="" name="experts_com_href[]"/>
    </td></tr>
    <tr><td class="form_name"></td><td>
        <input type="button" value="Thêm chuyên gia" class="expert-add"/>
    </td></tr>
    <? $form->close_table();?>
    <h4>Tóm tắt</h4>
    <textarea name="int_summary" class="form_control" id="int_summary" style="width:600px;height:100px;"><?=$record['int_summary']?></textarea>
    <?=$form->tinyMCE('Chi tiết', 'int_body', 'int_body', $record['int_body'], '80%', '300')?>
    <?
    $form->create_table();
    ?>
	<?=$form->button("submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "submit" . $form->ec . "reset", "Cập nhật" . $form->ec . "Làm lại", "Cập nhật" . $form->ec . "Làm lại", 'style="background:url(' . $fs_imagepath . 'button_1.gif) no-repeat"' . $form->ec . 'style="background:url(' . $fs_imagepath . 'button_2.gif)"', "");?>
	<?=$form->hidden("action", "action", "edit", "");?>
	<?
	$form->close_table();
	$form->close_form();
	unset($form);
	?>
	</div>
<?=template_bottom() ?>
</body>
</html>